import {MAX_CARD_ID, OrderCardStatus, OrderStatus} from '../GameDef';
import {CardPoolMgr} from '../manager/CardPoolMgr';
import {BaseCard, CardInOrder} from './Card';


/**
 * 卡牌类 - 表示一张扑克牌
 */
export class Order {

    private _uid: number;
    private _status: OrderStatus = OrderStatus.NONE;
    private _checkSuit: boolean;
    private _name: string;
    private _cards: CardInOrder[];
    private _missIndexs: number[];

    get uid(): number {
        return this._uid;
    }

    get status(): OrderStatus {
        return this._status;
    }

    set status(status: OrderStatus) {
        this._status = status;
    }

    get name(): string {
        return this._name;
    }

    get isFinished(): boolean {
        let ret = true;
        for(let i of this._missIndexs) {
            if (this._cards[i].status == OrderCardStatus.WAIT) {
                ret = false;
                break;
            }
        }
        return ret;
    }

    get cards(): CardInOrder[] {
        return this._cards;
    }

    get checkSuit(): boolean {
        return this._checkSuit;
    }

    constructor(uid: number, cardids: number[], suitCheck: boolean, name: string) {
        this._uid = uid;
        this._checkSuit = suitCheck;
        this._name = name;
        this._cards = [];
        this._missIndexs = [];

        for (let i = 0; i < cardids.length; i++) {
            const cardid = cardids[i];
            if (cardid > MAX_CARD_ID) {
                // 大于最大的牌id，说明是牌库的牌uid
                let cuid = cardid;
                let card = CardPoolMgr.getInstance().getCardById(cuid);
                let oc = new CardInOrder(cuid, card.cardId, this.uid, this._checkSuit, OrderCardStatus.WAIT);
                this._missIndexs.push(i);
                this._cards.push(oc);
            } else {
                let oc = new CardInOrder(-1, cardid, this.uid, this._checkSuit, OrderCardStatus.FINISH);
                this._cards.push(oc);
            }
        }
    }

    equals(order: Order): boolean {
        return this.uid === order.uid;
    }

    isFit(card: BaseCard): number {
        let ret = -1;

        // 先进行精确匹配
        for (const i of this._missIndexs) {
            const c = this._cards[i];
            if (c.isFit(card, true)) {
                ret = i;
                break;
            }
        }

        if (ret < 0) {
            // 再进行模糊匹配
            for (const i of this._missIndexs) {
                const c = this._cards[i];
                if (c.isFit(card, false)) {
                    ret = i;
                    break;
                }
            }
        }

        return ret;
    }

    setCard(card: BaseCard): number {
        let ret = -1;
        let pool = CardPoolMgr.getInstance();
        const index = this.isFit(card);
        if (index >= 0) {
            const c = this._cards[index];
            c.status = OrderCardStatus.FINISH;
            if (c.orderId != card.orderId) {
                // fix wrong card
                console.log(`setCard: fix wrong card: ${JSON.stringify(c)} => ${JSON.stringify(card)}`);
                ret = pool.fixWrongCard(c.uid, card.cardId, card.orderId);
            }
        }

        if (this.isFinished) {
            this._status = OrderStatus.FINISH;
        }
        return ret;
    }

    getNeedCardUids(): number[] {
        let ret = [];
        for (const i of this._missIndexs) {
            const c = this._cards[i];
            if (c.status == OrderCardStatus.WAIT) {
                ret.push(c.uid);
            }
        }

        return ret;
    }
}
